question.required ? [] : [index],
)}
>
{visibleQuestions.map((question, questionIndex) => {
let isDisabled = false;
if (question.logic?.dependsOn) {
const { title, values } = question.logic.dependsOn;
const dependentQuestionIndex = visibleQuestions.findIndex(
(q) => q.title === title,
);
if (dependentQuestionIndex !== -1) {
const answer = getAnswerValue(
visibleQuestions[dependentQuestionIndex],
dependentQuestionIndex,
);
isDisabled = !values.includes(String(answer));
}
}
const answer = getAnswerValue(question, questionIndex);
const hasAnswer = hasQuestionAnswerValue(answer ?? null);
let isAnswered = hasAnswer;
if (hasAnswer) {
const isEmailQuestion =
question.title.toLowerCase().includes("email") ||
question.title.includes("ایمیل");
if (isEmailQuestion) {
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
isAnswered = emailRegex.test(String(answer).trim());
}
}
return (
{renderQuestion(
question,
questionIndex,
isDisabled,
dobQuestion,
dobQuestionIndex,
)}
);
})}
);
}
export default function QuestionDetailClient({
closeLabel,
continueLabel,
description,
informationLabel,
itemSlug,
locale = defaultLocale,
questionsListHref,
title,
}: QuestionDetailClientProps) {
const router = useRouter();
const { dictionary: t } = useI18n();
const [isTestStarted, setIsTestStarted] = useState(false);
const { data: profile, isLoading: isProfileLoading } = useMarriageProfileQuery();
const profileGender = profile?.gender;
const age = getStoredAge();
const item = getQuestionListItemBySlug(itemSlug, locale);
const profileContext = useMemo(
() => ({
age,
gender: profileGender as MarriageGender | null | undefined,
}),
[age, profileGender],
);
const visibleQuestions = useMemo(() => {
if (!item) {
return [];
}
const hasDobQuestion = item.questions.some(
(q) => q.title === "Date of Birth" || q.title === "تاریخ تولد",
);
return item.questions
.filter((question) => {
if (
hasDobQuestion &&
(question.title === "Age" || question.title === "سن")
) {
return false;
}
return isQuestionVisibleForProfile(question, profileContext);
})
.map((question) => ({
...question,
required: isQuestionRequiredForProfile(question, profileContext),
}));
}, [item, profileContext]);
const requiredQuestionsCount = useMemo(
() => visibleQuestions.filter((q) => q.required).length,
[visibleQuestions],
);
useEffect(() => {
if (isProfileLoading) {
return;
}
if (!item || isQuestionListItemVisibleForProfile(item, profileContext)) {
return;
}
router.replace(questionsListHref);
}, [isProfileLoading, item, profileContext, questionsListHref, router]);
if (isProfileLoading && item) {
// Show component skeleton/layout structure while loading profile
} else if (!item || !isQuestionListItemVisibleForProfile(item, profileContext)) {
return null;
}
if (item && item.questions.length === 0) {
if (isTestStarted) {
const testQuestions: TestQuestion[] =
item.slug === "glasser_5_needs_test"
? [
{
id: 1,
text: "How important is financial security and long-term stability in your life?",
options: [
{ label: "Very Low", value: 1 },
{ label: "Low", value: 2 },
{ label: "Moderate", value: 3 },
{ label: "High", value: 4 },
{ label: "Very High", value: 5 },
],
},
{
id: 2,
text: "To what extent do you value personal freedom and flexibility in daily choices?",
options: [
{ label: "Very Low", value: 1 },
{ label: "Low", value: 2 },
{ label: "Moderate", value: 3 },
{ label: "High", value: 4 },
{ label: "Very High", value: 5 },
],
},
{
id: 3,
text: "How much do you seek recognition and personal achievement in your career/life?",
options: [
{ label: "Very Low", value: 1 },
{ label: "Low", value: 2 },
{ label: "Moderate", value: 3 },
{ label: "High", value: 4 },
{ label: "Very High", value: 5 },
],
},
{
id: 4,
text: "How essential is fun, humor, and playfulness in your relationship?",
options: [
{ label: "Very Low", value: 1 },
{ label: "Low", value: 2 },
{ label: "Moderate", value: 3 },
{ label: "High", value: 4 },
{ label: "Very High", value: 5 },
],
},
{
id: 5,
text: "How deep is your need for emotional connection and belonging with a partner?",
options: [
{ label: "Very Low", value: 1 },
{ label: "Low", value: 2 },
{ label: "Moderate", value: 3 },
{ label: "High", value: 4 },
{ label: "Very High", value: 5 },
],
},
]
: [
{
id: 1,
text: "How many hours do you typically spend on social media each day?",
options: [
{ label: "Between 11 and 15 Years", value: "11_15" },
{ label: "Between 16 and 25 Years", value: "16_25" },
{ label: "Between 26 and 40 Years", value: "26_40" },
{ label: "Over 40 Years", value: "over_40" },
],
},
{
id: 2,
text: "I prefer spending my free time engaging in quiet, reflective activities.",
options: [
{ label: "Strongly Agree", value: "strongly_agree" },
{ label: "Agree", value: "agree" },
{ label: "Neutral / Uncertain", value: "neutral" },
{ label: "Disagree", value: "disagree" },
],
},
{
id: 3,
text: "When making important life decisions, I rely more on logical analysis than feelings.",
options: [
{ label: "Always Logical", value: "always_logical" },
{ label: "Mostly Logical", value: "mostly_logical" },
{ label: "Balanced", value: "balanced" },
{ label: "Mostly Intuitive", value: "mostly_intuitive" },
],
},
{
id: 4,
text: "How do you handle unexpected changes to your planned routine?",
options: [
{ label: "Adapt quickly and calmly", value: "adapt_quick" },
{ label: "Need a moment to adjust", value: "need_moment" },
{ label: "Feel stressed but manage", value: "stressed" },
{ label: "Prefer strict adherence to plans", value: "strict_plans" },
],
},
{
id: 5,
text: "In social gatherings, I usually initiate conversations with new acquaintances.",
options: [
{ label: "Very True", value: "very_true" },
{ label: "Somewhat True", value: "somewhat_true" },
{ label: "Rarely True", value: "rarely_true" },
{ label: "Not True At All", value: "not_true" },
],
},
];
return (